package com.yuyu.qingmangmall.order.web;

import com.rabbitmq.client.Channel;
import com.yuyu.qingmangmall.order.entity.OrderEntity;
import com.yuyu.qingmangmall.order.entity.OrderReturnReasonEntity;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.UUID;

//@RabbitListener(queues = {"hello-java-queue"})
@Controller
public class HelloController {

    @Autowired
    RabbitTemplate rabbitTemplate;

    @GetMapping("/{page}.html")
    public String listPage(@PathVariable("page") String page){


        return page;
    }


    @ResponseBody
    @GetMapping("/test/createOrder")
    public String createOrderTest(){

        OrderEntity orderEntity = new OrderEntity();
        orderEntity.setOrderSn(UUID.randomUUID().toString());
        orderEntity.setModifyTime(new Date());
        // 发送请求，如果发送的消息是个对象  会使用序列化机制，将对象写出去。对象必须实现Serializable
        rabbitTemplate.convertAndSend("order-event-exchange","order.create.order",orderEntity);
        return "ok";
    }



    /*
    queues :声明需要监听的所有队列
    org.springframework.amqp.core.Message

    参数可以写以下类型
    1. Message message 原生消息详细信息。头+体
    2. T<发送的消息类型> OrderReturnReasonEntity reasonEntity
    3. Channel channel：当前传输的通道

    Queue: 可以很多人都来监听。只要收到消息，队列删除消息，而且只能有一个收到此消息
    场景：
        1）、订单服务开启多个，同一个消息，只能有一个客户端收到
        2）、只有一个消息完全处理完，方法运行结束，我们就可以接受下一个消息
 */
    @RabbitListener(queues = {"hello-java-queue"})
    @RabbitHandler
    void receiveMessage(Message message,
                        OrderReturnReasonEntity reasonEntity,
                        Channel channel){

        byte[] body = message.getBody();
        // 消息头属性信息
        MessageProperties properties = message.getMessageProperties();

//        System.out.println("message:"+message);
//        System.out.println("message类型："+message.getClass());
//        System.out.println("message属性："+properties);
        System.out.println("reasonEntity: "+reasonEntity);
        // channel内按顺序自增的。
        long deliveryTag = message.getMessageProperties().getDeliveryTag();

        // 签收货物，非批量模式
        try {
            if (deliveryTag%2==0){
                channel.basicAck(deliveryTag,false);
                System.out.println("签收了货物。。。"+deliveryTag);
            } else {
                // long deliveryTag,
                // boolean multiple,
                // boolean requeue
                channel.basicNack(deliveryTag,false,false);
                System.out.println("拒签。。。"+deliveryTag);
            }

        }catch (Exception e){
            // 网络中断
        }
    }

    @RabbitListener(queues = {"hello-java-queue"})
    @RabbitHandler
    void receiveMessage2(Message message,
                         OrderEntity orderEntity,
                         Channel channel){

        byte[] body = message.getBody();
        MessageProperties properties = message.getMessageProperties();

//        System.out.println("message:"+message);
//        System.out.println("message类型："+message.getClass());
//        System.out.println("message属性："+properties);
        System.out.println("reasonEntity: "+orderEntity);
    }

}
